﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using TextMIS;
using System.Collections;

namespace TextMIS
{
    public partial class frmItems : Form
    {
        public frmItems()
        {
            InitializeComponent();
        }

        private void frmItems_Load(object sender, EventArgs e)
        {
            //new Common.Common().AddImages(imageList1);
            Tr_ItmList.ImageList = imageList1;
            //Tr_ItmList.ImageIndex = 1;
            DataBind(); 
        }
        void DataBind()
        {
            Tr_ItmList.Nodes.Clear();
            //str_JykCode = xtbiz.GetXtCsz("JykCode");//检验科代码

            AddTree();

            cb_Bran.SelectedIndexChanged -= new EventHandler(cb_Bran_SelectedIndexChanged);
            cb_Bran.DataSource = ItemDB.Get_Bran_list();
            cb_Bran.DisplayMember = "Bra_name";
            cb_Bran.ValueMember = "Bra_id";
            cb_Bran.SelectedIndexChanged += new EventHandler(cb_Bran_SelectedIndexChanged);

            cb_sex.DataSource = NodeSys.GetDicInfo(1);//性别-系统字典获取
            cb_sex.DisplayMember = "xmmc";
            cb_sex.ValueMember = "bzdm";
        }


        sysopr NodeSys = new sysopr();
        //ywszBiz ywszbiz = new ywszBiz();

        dbopr ItemDB = new dbopr();
        //string str_JykCode = "";//检验科代码
        string str_state = "Update";
        private TreeNode treeNode = null;

        void AddTree()
        {
            Tr_ItmList.Nodes.Clear();
            TreeNode node = new TreeNode("体检科室");
            node.Tag = "0";
            DataTable dt_Bran = ItemDB.Get_Bran_list();
            foreach (DataRow dr in dt_Bran.Rows)
            {
                string str_Bra = dr["Bra_id"].ToString().Trim();//类型编号（科室）
                TreeNode node1 = new TreeNode(dr["Bra_name"].ToString().Trim());
                node1.Tag = str_Bra;

                //if (str_JykCode == str_lxbh)
                //{
                //    DataTable dt_lclxb = ItemDB.Get_tj_lclxb();
                //    foreach (DataRow dr3 in dt_lclxb.Rows)
                //    {
                //        TreeNode node3 = new TreeNode(dr3["mc"].ToString().Trim());
                //        node3.Tag = dr3["lclx"].ToString().Trim();

                //        DataTable dt_tjxmb = ItemDB.Get_tj_tjxmb(str_lxbh, node3.Tag.ToString());
                //        foreach (DataRow dr4 in dt_tjxmb.Rows)
                //        {
                //            TreeNode node4 = new TreeNode(dr4["mc"].ToString().Trim());
                //            node4.Tag = dr4["tjxm"].ToString().Trim();
                //            //node4.StateImageIndex = 0;
                //            //node4.SelectedImageIndex = 0;
                //            //node4.ImageIndex = 0;
                //            //tv_tjlxb.ImageIndex = 0;
                //            if (dr4["qybz"].ToString().Trim() == "0")
                //            {
                //                //node4.StateImageIndex = 1;
                //                //node4.SelectedImageIndex = 1;
                //                //node4.ImageIndex = 1;
                //                //tv_tjlxb.ImageIndex = 1;
                //                node4.ForeColor = Color.Red;
                //            }
                //            node3.Nodes.Add(node4);
                //        }
                //        node1.Nodes.Add(node3);
                //    }

                //}
                //else
                //{
                    DataTable Itmls = ItemDB.Get_ItemList(str_Bra);
                    foreach (DataRow dr1 in Itmls.Rows)
                    {
                        TreeNode node2 = new TreeNode(dr1["Item_name"].ToString().Trim());
                        node2.Tag = dr1["Item_id"].ToString().Trim();
                        if (dr1["IsEnable"].ToString().Trim() == "0") node2.ForeColor = Color.Red;
                        node1.Nodes.Add(node2);
                    }
                //}

                node.Nodes.Add(node1);
            }
            Tr_ItmList.Nodes.Add(node);
            AddImage(node);

            //Tr_ItmList.Nodes.Expand();
        }

        /// <summary>
        /// 为树添加图标
        /// </summary>
        /// <param name="tn"></param>
        void AddImage(TreeNode tn)
        {
            foreach (TreeNode node in tn.Nodes)
            {
                if (node.Nodes.Count > 0)
                {
                    if (node.IsExpanded)
                    {
                        node.ImageIndex = 0;
                    }
                    else
                    {
                        node.ImageIndex = 1;
                    }
                    AddImage(node);
                }
                else
                {

                    if (node.ForeColor == Color.Red)
                    {
                        node.ImageIndex = 3;
                    }
                    else
                    {
                        node.ImageIndex = 2;
                    }
                }
            }
        }

        

        void cb_Bran_SelectedIndexChanged(object sender, EventArgs e)
        {
            //if (str_JykCode == cb_Bran.SelectedValue.ToString().Trim())
            //{
            //cb_Bran.Enabled = true;
            //cb_Bran.DataSource = ItemDB.Get_Bran_list();
            //cb_Bran.DisplayMember = "Bra_name";
            //cb_Bran.ValueMember = "Bra_id";
            //}
            //else
            //{
            //    cb_Bran.DataSource = null;
            //    cb_Bran.Enabled = false;
            //}
        }


        private void rb_Char_CheckedChanged(object sender, EventArgs e)
        {
            if (rb_Char.Checked)//文字
            {
                tb_Unit.Text = "";
                tb_minvalue.Text = "";
                tb_maxvalue.Text = "";
                tb_Unit.ReadOnly = true;
                tb_minvalue.ReadOnly = true;
                tb_maxvalue.ReadOnly = true;
            }
        }

        private void rb_Num_CheckedChanged(object sender, EventArgs e)
        {
            if (rb_Num.Checked)//数字
            {
                tb_Unit.ReadOnly = false;
                tb_minvalue.ReadOnly = false;
                tb_maxvalue.ReadOnly = false;
            }
        }

        private void Tr_ItmList_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (object.Equals(null, Tr_ItmList.SelectedNode.Tag)) return;
            string str_itemid = Tr_ItmList.SelectedNode.Tag.ToString().Trim();
            DataTable dt = ItemDB.Get_Item_Info(str_itemid);

            //Bra_id,Item_id,Item_name,dj,Disp_order,SexAttr,lclx,Item_unit,Result_Norm,Result_Lo,Result_Up,zcts,IsEnable,LoLimited,UpLimited
            if (dt.Rows.Count < 1) return;
            cb_Bran.SelectedValue = dt.Rows[0]["Bra_id"].ToString().Trim();
            tb_Price.Text = dt.Rows[0]["Item_Price"].ToString().Trim();
            tb_ItmId.Text = dt.Rows[0]["Item_id"].ToString().Trim();
            tb_disp_order.Text = dt.Rows[0]["Disp_order"].ToString().Trim();
            tb_name.Text = dt.Rows[0]["Item_name"].ToString().Trim();
            cb_sex.SelectedValue = dt.Rows[0]["SexAttr"].ToString().Trim();
            cb_MedTyp.SelectedValue = dt.Rows[0]["lclx"].ToString().Trim();

            if (dt.Rows[0]["Result_Type"].ToString().Trim() == "0")
                rb_Char.Checked = true;
            if (dt.Rows[0]["Result_Type"].ToString().Trim() == "1")
                rb_Num.Checked = true;

            if (dt.Rows[0]["IsEnable"].ToString().Trim() == "1")
                rb_Enable.Checked = true;
            if (dt.Rows[0]["IsEnable"].ToString().Trim() == "0")
                rb_Unable.Checked = true;

            tb_Unit.Text = dt.Rows[0]["Item_unit"].ToString().Trim();
            tb_NormRes.Text = dt.Rows[0]["Result_Norm"].ToString().Trim();
            tb_minvalue.Text = dt.Rows[0]["LoLimited"].ToString().Trim();
            tb_maxvalue.Text = dt.Rows[0]["UpLimited"].ToString().Trim();
            str_state = "Update";
            cb_Bran.Enabled = false;
            this.treeNode = Tr_ItmList.SelectedNode;

            if (Tr_ItmList.SelectedNode.Nodes.Count <= 0)
            {
                Tr_ItmList.SelectedNode.SelectedImageIndex = Tr_ItmList.SelectedNode.ImageIndex;
                Tr_ItmList.SelectedNode.Parent.ImageIndex = 0;
            }

        }

        void ClearControl()
        {
            tb_Price.Text = "0.00";
            tb_ItmId.Text = "";
            tb_disp_order.Text = "";
            tb_name.Text = "";
            cb_sex.SelectedValue = "%";
            tb_Unit.Text = "";
            tb_NormRes.Text = "";
            rb_Char.Checked = true; //默认字符
            rb_Enable.Checked = true;  //默认启用
            tb_minvalue.Text = "";
            tb_maxvalue.Text = "";
        }

        private void bt_add_Click(object sender, EventArgs e)
        {
            ClearControl();
            cb_Bran.Enabled = true;
            str_state = "Insert";
        }

        

        private void bt_save_Click(object sender, EventArgs e)
        {

            string str_ResTyp = "0";
            string str_Enable = "1";
            string str_lclx = "";//临床类型
            if (rb_Char.Checked) str_ResTyp = "0";//字符
            if (rb_Num.Checked) str_ResTyp = "1";//数值
            if (rb_Enable.Checked) str_Enable = "1";//启用
            if (rb_Unable.Checked) str_Enable = "0";//停用
            if (object.Equals(null, cb_MedTyp.SelectedValue))
            {
                str_lclx = "";
            }
            else
            {
                str_lclx = cb_MedTyp.SelectedValue.ToString().Trim();
            }
            if (str_state == "Update")
            {
                
                if (tb_ItmId.Text.Trim() == "") return;
                ItemDB.Update_Iteminfo(cb_Bran.SelectedValue.ToString().Trim(), tb_ItmId.Text.Trim(), tb_name.Text.Trim(), tb_Price.Text.Trim(),
                    tb_disp_order.Text.Trim(), cb_sex.SelectedValue.ToString().Trim(), str_ResTyp, tb_Unit.Text.Trim(),
                    tb_NormRes.Text.Trim(), str_lclx, str_Enable, tb_minvalue.Text.Trim(), tb_maxvalue.Text.Trim());
                MessageBox.Show("修改成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                if (rb_Unable.Checked)
                {
                    Tr_ItmList.SelectedNode.ForeColor = Color.Red;
                    Tr_ItmList.SelectedNode.ImageIndex = 3;
                    Tr_ItmList.SelectedNode.SelectedImageIndex = 3;
                }
                else
                {
                    Tr_ItmList.SelectedNode.ForeColor = Tr_ItmList.Nodes[0].ForeColor;
                    Tr_ItmList.SelectedNode.ImageIndex = 2;
                    Tr_ItmList.SelectedNode.SelectedImageIndex = 2;
                }

                Tr_ItmList.SelectedNode.Text = tb_name.Text.Trim();
            }
            if (str_state == "Insert")
            {
                string sex = cb_sex.SelectedValue.ToString();
                if (object.Equals(cb_Bran.SelectedValue, null)) return;

                tb_ItmId.Text = ItemDB.Get_Pro_ItemId(cb_Bran.SelectedValue.ToString().Trim());//获取体检项目的编号通过存储过程
                if (tb_disp_order.Text == "")
                    tb_disp_order.Text = tb_ItmId.Text;
                //Bra_id,Item_id,Item_name,dj,Disp_order,SexAttr,lclx,Item_unit,Result_Norm,Result_Lo,Result_Up,zcts,IsEnable,LoLimited,UpLimited
                ItemDB.Insert_Iteminfo(cb_Bran.SelectedValue.ToString().Trim(), tb_ItmId.Text.Trim(), tb_name.Text.Trim(), tb_Price.Text.Trim(),
                    tb_disp_order.Text.Trim(), sex, "", tb_Unit.Text.Trim(),
                    tb_NormRes.Text.Trim(), "1", "", tb_minvalue.Text.Trim(), tb_maxvalue.Text.Trim());

                MessageBox.Show("保存成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                
                str_state = "Update";
                TreeNode node = new TreeNode(tb_name.Text.Trim());
                node.Tag = tb_ItmId.Text.Trim();
                if (rb_Unable.Checked) node.ForeColor = Color.Red;
                Tr_ItmList.SelectedNode.Parent.Nodes.Add(node);
                cb_Bran.Enabled = false;

            }

            AddTree();
            this.treeNode = Tr_ItmList.SelectedNode;
            if (treeNode != null)
            {
                treeNode.Expand();
            }
        }

        void Remove_Node(string tag)
        {
            foreach (TreeNode node in Tr_ItmList.Nodes)
            {
                if (node.Tag.ToString().Trim() == tag)
                {
                    Tr_ItmList.Nodes.Remove(node);
                    break;
                }
            }
        }
        private void bt_delete_Click(object sender, EventArgs e)
        {
            if (tb_ItmId.Text.Trim() == "") return;
            //注意项目引用了，就不允许删除，暂时没有控制
            //if (ItemDB.Exists_Tjxm(tb_ItmId.Text.Trim()) > 0)
            //{
            //    MessageBox.Show("该项目被引用了，不允许删除！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //    return;
            //}

            ItemDB.Delete_Iteminfo(tb_ItmId.Text.Trim());
            MessageBox.Show("删除成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            Tr_ItmList.Nodes.Remove(Tr_ItmList.SelectedNode);
            ClearControl();
        }
        private void bt_exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
